#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from google.appengine.ext import webapp, db
from google.appengine.ext.webapp import util
from google.appengine.api import users
from data import Problem, User
import os
from google.appengine.ext.webapp import template

class LoginHandler(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            self.redirect("main")
        else:
            greeting = ("<a href=\"%s\">Sign in or register</a>." %
                        users.create_login_url("/"))

            self.response.out.write("<html><body>%s</body></html>" % greeting)

class MainHandler(webapp.RequestHandler):
    def get(self):
        try:
            user = db.GqlQuery("select * from User where user = :1", users.get_current_user()).fetch(1)[0].user
        except IndexError:
            user = User()
            user.user = users.get_current_user()
            user.level = 0
            user.put()
        admin = users.is_current_user_admin()
        logout = users.create_logout_url("/")
        q = db.GqlQuery("select * from Problem")
        problems = q.fetch(100)
        template_values = { "problems" : problems, "user" : user, "admin" : admin, "logout" : logout }
        path = os.path.join(os.path.dirname(__file__), 'main.html')
        self.response.out.write(template.render(path, template_values))

class CreateNewProblem(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        admin = users.is_current_user_admin()
        template_values = { "user" : user, "admin" : admin }
        path = os.path.join(os.path.dirname(__file__), 'CreateNewProblem.html')
        self.response.out.write(template.render(path, template_values))


class SaveProblem(webapp.RequestHandler):
    def post(self):
        title = self.request.get('title')
        codebody = self.request.get('codebody').replace("\r","")
        variables = self.request.get('variables')
        
        level = int(self.request.get('level'))
        p=Problem()
        p.level = level
        p.title = title
        p.codebody = codebody
        p.variables = variables
        p.user = users.get_current_user()
        p.put()
        self.redirect("main")
def main():
    application = webapp.WSGIApplication([('/', LoginHandler),
                                          ('/main', MainHandler),
                                          ('/CreateNewProblem', CreateNewProblem),
                                          ('/SaveProblem', SaveProblem)],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()
